import Vue from "vue";
import Vuex from "vuex";
import VueCookie from "vue-cookie";
import { setItem, getItem } from "@/utils/storage.js";
// 1.安装插件
Vue.use(Vuex);

// 2.创建对象
const store = new Vuex.Store({
  state: { // 状态集合
    // 登录的用户
    loginUserId: VueCookie.get("uid"),
    // token
    tokenInfo: getItem("tokenInfo") || {}
  },
  mutations: {
    mSetTokenInfo(state, tokenObj) {
      state.tokenInfo = tokenObj;
      // 因为刷新会丢失所以进行持久化，调用storage文件里方法
      setItem("tokenInfo", tokenObj);
    },
    mSetUserId(state, userId) {
      state.loginUserId = userId;
      VueCookie.set("uid", userId);
    }
  },
  getters: {
    getLoginUserId: state => state.loginUserId,
    getToken: state => state.tokenInfo
  }
});

// 3.导出store对象
export default store